# local modules
import geom
geom = reload( geom )
import load
load = reload( load )
# local constants
from constants import *
def read_and_map_nodes( self ):
    localTOL = TOL*100.
    zs = []
    thetas = []
    tmpnodes = []
    for node in self.part.nodes:
        x, y, z = node.coordinates
        r, theta, z = load.rec2cyl( x, y, z )
        newNode = geom.Node()
        newNode.x = x
        newNode.y = y
        newNode.z = z
        newNode.r = r
        newNode.theta = theta
        newNode.obj = node 
        newNode.z_index = None
        newNode.theta_index = None
        tmpnodes.append( newNode )
        self.nodes[ newNode.id() ] = newNode
        #
        consider = True
        for thetai in thetas:
            if abs(thetai - theta) < localTOL:
                consider = False
                break
        if consider:
            thetas.append( theta )
        #
        consider = True
        for zi in zs:
            if abs(zi - z) < localTOL:
                consider = False
                break
        if consider:
            zs.append( z )
    #
    thetas.sort()
    zs.sort()
    for node in tmpnodes:
        for z_index in range(len(zs)):
            z = zs[ z_index ]
            if abs(node.z - z) < localTOL:
                node.z_index = z_index
                break        
        for theta_index in range(len(thetas)):
            theta = thetas[ theta_index ]
            if abs(node.theta - theta) < localTOL:
                node.theta_index = theta_index
                break

    self.cross_sections = []
    for z_index in range(len(zs)):
        z = zs[z_index]
        cross_section = geom.CrossSection()
        cross_section.z = z
        cross_section.index = z_index
        cross_section.thetas = thetas
        cross_section.nodes = [None for i in range(len(thetas))]
        #TODO slowness spot
        for node in tmpnodes:
            if node.z_index == z_index:
                node.cross_section = cross_section
                cross_section.nodes[ node.theta_index ] = node
        self.cross_sections.append( cross_section )

    self.meridians = []
    for theta_index in range(len(thetas)):
        theta = thetas[ theta_index ]
        meridian = geom.Meridian()
        meridian.index = theta_index
        meridian.theta = theta
        meridian.zs = zs
        meridian.nodes = [None for i in range(len(zs))]
        #TODO slowness spot
        for node in tmpnodes:
            if node.theta_index == theta_index:
                node.meridian = meridian
                meridian.nodes[ node.z_index ] = node
        self.meridians.append( meridian )

    for pload in self.ploadconf.ploads:
        pload.get_node()
